package com.recordhistory.service;

import com.recordhistory.dao.ReignTitleDao;
import com.recordhistory.exception.ServiceException;
import com.recordhistory.model.ReignTitle;
import com.recordhistory.utils.CheckTool;
import com.recordhistory.utils.CommonTool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：LX
 * 创建时间： 2019/6/18. 9:03
 * 地点：广州
 * 目的: 年号
 * 备注说明：
 */
@Service
public class ReignTitleService {

    @Resource
    private ReignTitleDao reignTitleDao;





    /**
     * 年号相关校验
     * @param reignTitle
     */
    private void checkReignTitle(ReignTitle reignTitle){
        if (reignTitle == null){
            throw new ServiceException("数据不能为空");
        }
        if (CheckTool.checkNull(reignTitle.getHistoryId())){
            throw new ServiceException("请选择历史主线");
        }
        if (CheckTool.checkNull(reignTitle.getRegin())){
            throw new ServiceException("年号不能为空");
        }
        if (CheckTool.checkNull(reignTitle.getEmperor())){
            throw new ServiceException("请输入帝王名称");
        }

        ReignTitle sqlReignTitle = reignTitleDao.selectReignByReginNotThis(reignTitle.getId(), reignTitle.getRegin(), reignTitle.getHistoryId());
        if (sqlReignTitle != null){
            throw new ServiceException("年号已经存在");
        }
    }

    /**
     * 查找 帝王年号
     * @param id 主键
     * @param regin 年号
     * @param historyId 历史主线ID
     * @return
     */
    public ReignTitle selectReignByReginNotThis(String id, String regin, String historyId){
        return reignTitleDao.selectReignByReginNotThis(id, regin, historyId);
    }

    /**
     * 添加年号
     * @param reignTitle
     */
    @Transactional(rollbackFor = Exception.class)
    public void addReignTitle(ReignTitle reignTitle){
        checkReignTitle(reignTitle);

        reignTitle.setId(CommonTool.getUUId());
        reignTitle.setCreateDate(CommonTool.getTimestamp());
        reignTitle.setUpdateDate(CommonTool.getTimestamp());

        reignTitleDao.insert(reignTitle);
    }

    /**
     * 批量保存
     * @param titleList
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<ReignTitle> titleList) {
        for (ReignTitle reignTitle : titleList){
            reignTitleDao.insert(reignTitle);
        }
    }

    /**
     * 更新年号
     * @param reignTitle
     */
    @Transactional(rollbackFor = Exception.class)
    public void updReignTitle(ReignTitle reignTitle){
        checkReignTitle(reignTitle);

        if (CheckTool.checkNull(reignTitle.getId())){
            throw new ServiceException("主键为空，不能更新");
        }
        reignTitle.setUpdateDate(CommonTool.getTimestamp());

        reignTitleDao.updateById(reignTitle);
    }


    /**
     * 删除年号
     * @param id 年号主键
     */
    @Transactional(rollbackFor = Exception.class)
    public void delReignTitle(String id){
        if (CheckTool.checkNull(id)){
            throw new ServiceException("ID不能为空");
        }

        reignTitleDao.deleteById(id);
    }

    /**
     * 根据 主键查询年号
     * @param id 年号主键
     * @return
     */
    public ReignTitle selectById(String id){
        return reignTitleDao.selectById(id);
    }


    /**
     * 根据历史主线找年号
     * @param historyId 历史主线ID
     * @return 年号
     */
    public List<ReignTitle> selecByHtistoryId(String historyId){
        return reignTitleDao.selecByHtistoryId(historyId);
    }


}
